public class App {
    public static void main(String[] args) throws Exception {
        System.out.println("Hello, World!");
        int[] a = {1,5,200};
        int[] b = {1, 2, 3};

        int max = maxMuti(b, a);
        System.out.println("maxMuti=" + max);
    }

    /**
     * 两个正整数数组，A和B，规则是顺序从B里取出值B(n),从A的头或尾部取出一个值(A可以看做一个双向链表),取出的值相乘，然后求把A数组取完后所有乘积和的最大值
     */
    static int maxMuti(int[] arrB, int[] arrA) {
        int lengthB = arrB.length;
        int lengthA = arrA.length;
        int maxMuti = 0;

        int firstIndex = 0;
        int lastIndex = lengthA - 1;
        int max = 0;

        for (int i = 0; i < lengthB; i++) {
            if(i >= lengthA) break;

            if(firstIndex <= lengthA - 1 && lastIndex >= 0) {
                if(arrA[firstIndex] < arrA[lastIndex]) {
                    max = arrA[lastIndex];
                    lastIndex--;
                }else{
                    max = arrA[firstIndex];
                    firstIndex++;
                }
            }
            
            System.out.println("max=" + max + "  b=" + arrB[i]);
            maxMuti += max * arrB[i];
        }

        return maxMuti;
    }

    /**
     * 两个正整数数组，A和B，规则是顺序从B里取出值B(n),从A的头或尾部取出一个值(A可以看做一个双向链表),取出的值相乘，然后求把A数组取完后所有乘积和的最大值
     */
    static int maxMuti1(int[] arrB, int[] arrA) {
        int lengthB = arrB.length;
        int lengthA = arrA.length;
        int maxMuti = 0;

        int firstIndex = 0;
        int lastIndex = lengthA - 1;
        int max = 0;

        for (int i = 0; i < lengthB; i++) {
            if(i >= lengthA) break;

            if(firstIndex <= lengthA - 1 && lastIndex >= 0) {
                if(arrA[firstIndex] < arrA[lastIndex]) {
                    max = arrA[lastIndex];
                    lastIndex--;
                }else{
                    max = arrA[firstIndex];
                    firstIndex++;
                }
            }
            
            System.out.println("max=" + max + "  b=" + arrB[i]);
            maxMuti += max * arrB[i];
        }

        return maxMuti;
    }
}
